package 数青蛙;

import java.util.HashMap;
import java.util.Map;

class Solution {
    //基础if语句
    public int minNumberOfFrogs(String croakOfFrogs) {
        int[] hash = new int[5];
        for(int i = 0; i < croakOfFrogs.length(); i++){
            if(croakOfFrogs.charAt(i) == 'c'){
                if(hash[4] == 0){
                    hash[0]++;
                }else{
                    hash[4]--;
                    hash[0]++;
                }
            }else{
                switch (croakOfFrogs.charAt(i)){
                    case 'r':
                        if(hash[0] == 0){
                            return -1;
                        }else{
                            hash[0]--;
                            hash[1]++;
                        }
                        break;
                    case 'o':
                        if(hash[1] == 0){
                            return -1;
                        }else{
                            hash[1]--;
                            hash[2]++;
                        }
                        break;
                    case 'a':
                        if(hash[2] == 0){
                            return -1;
                        }else{
                            hash[2]--;
                            hash[3]++;
                        }
                        break;
                    case 'k':
                        if(hash[3] == 0){
                            return -1;
                        }else{
                            hash[3]--;
                            hash[4]++;
                        }
                        break;
                    default: return -1;
                }
            }
        }
        for(int i = 0; i < 4; i++){
            if(hash[i] != 0){
                return -1;
            }
        }
        return hash[4];
    }

    //hash映射法
    public int minNumberOfFrogs2(String croakOfFrogs) {
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n];
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++){
            map.put(t.charAt(i),i);
        }
        char[] str = croakOfFrogs.toCharArray();
        for (char ch : str){
            if (ch == t.charAt(0)){
                if (hash[n - 1] != 0){
                    hash[n - 1]--;
                }
                hash[0]++;
            }else {
                int tmp = map.get(ch);
                if (hash[tmp - 1] == 0){
                    return -1;
                }
                hash[tmp - 1]--;
                hash[tmp]++;
            }
        }
        for (int i = 0; i < n - 1; i++){
            if (hash[i] != 0){
                return -1;
            }
        }
        return hash[n - 1];
    }
}